package com.legoo.studaychineseonline.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.legoo.studaychineseonline.R;
import com.legoo.studaychineseonline.base.BaseFragment;
import com.legoo.studaychineseonline.bean.LoginBean;
import com.legoo.studaychineseonline.http.API;
import com.legoo.studaychineseonline.http.XUtil;
import com.legoo.studaychineseonline.utils.EncryptionUtil;
import com.legoo.studaychineseonline.utils.IsNetWork;
import com.legoo.studaychineseonline.utils.RegularUtil;

import org.xutils.common.Callback;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class LoginFragment extends BaseFragment implements View.OnFocusChangeListener {
    Unbinder unbinder;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.ll_tiaokuan)
    LinearLayout llTiaokuan;
    @BindView(R.id.bt_login)
    Button btLogin;
    Unbinder unbinder1;
    private boolean passwordIsVis = true;
    private String iphoneNumber = "";
    private String sha;

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        unbinder = ButterKnife.bind(this, view);
        if (IsNetWork.isNetworkAvailable(context)) {

        }
        unbinder = ButterKnife.bind(this, view);
        etName.setOnFocusChangeListener(this);
        Bundle arguments = getArguments();
        if (arguments!=null){
            iphoneNumber = arguments.getString("iphoneNumber","");
            if (iphoneNumber != null && !"".equals(iphoneNumber)) {
                etName.setText(iphoneNumber);
            }
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_login;
    }


    @OnClick({R.id.ll_tiaokuan, R.id.bt_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_tiaokuan:
                break;
            case R.id.bt_login:

                String userName = etName.getText().toString().trim();
                if (!RegularUtil.isTelUserName(userName)) {
                    showToast("请输入4-18位英文或数字的用户名");
                    return;
                }
                String password = etPassword.getText().toString().trim();
                if (password.length() < 4 && password.length() > 20) {
                    showToast("请输入6-20位密码");
                    return;
                }
                if ("".equals(userName) || "".equals(password)) {
                    showToast("账号或密码不能为空");
                    return;
                }
                try {
                    sha = EncryptionUtil.getSHA(etPassword.getText().toString().trim());
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
                if ("".equals(sha) || sha == null) {
                    showToast("请重新输入密码");
                    return;
                }
                showPro();
                login(userName, sha);
                break;
            default:
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.et_name:
                break;
            default:

        }

    }

    private void login(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        XUtil.Post(API.LOGIN, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LoginBean loginBean = new Gson().fromJson(result, LoginBean.class);
                if (loginBean.getCode() == API.HTTP_OK) {
                    API.TOKEN = loginBean.getData().getToken().getToken();
                    API.USER_ID = loginBean.getData().getClient().getId();
                    getActivity().finish();
                }
                hidePro();
                showToast(loginBean.getMessage());

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                hidePro();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


}
